package com.epro.dx.activity.order;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Window;
import android.widget.Toast;

import com.epro.dx.R;
import com.epro.dx.activity.BaseFragmentActivity;
import com.epro.dx.activity.order.fragment.MyOrderFragment;
import com.epro.dx.adapter.MyOrderAdapter;
import com.epro.dx.listenner.OrderOperateCompleteListenner;
import com.epro.dx.widget.PagerSlidingTabStrip;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.HitBuilders.ScreenViewBuilder;
import com.google.android.gms.analytics.Tracker;
import com.weipu.common.DXApplication;
import com.weipu.common.DXApplication.TrackerName;

/**
 * 商品订单列表 123@123.com/123456
 * 
 * @author qsc255
 * @version [版本号, 2015-1-26]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class MyOrdersActivity extends BaseFragmentActivity implements
		OrderOperateCompleteListenner {

	private static final String TAG = "MyNewOrdersActivity";
	// 视图容器
	private ViewPager orderViewPager;
	// tab列表
	// private TabPageIndicator indicator;
	private PagerSlidingTabStrip indicator;

	// tab名称集合
	private String[] tabs = { "All", "Pending\nPayment", "Pending\nDelivery",
			"Full\nShipment" };

	private String[] status = { "All", "PendingPayment", "PendingDelivery",
			"FullShipment" };

	List<MyOrderFragment> newArrivalFragmentsLists = new ArrayList<MyOrderFragment>();

	// ViewPager的数据适配器
	private MyOrderAdapter adapter;
	
	private int fragmentIndex;
	private Tracker tracker;
	private ScreenViewBuilder screenViewBuilder;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		tracker = DXApplication.getContext().getTracker(TrackerName.APP_TRACKER);
		screenViewBuilder = new HitBuilders.ScreenViewBuilder();
		Intent intent=getIntent();
		if(null==intent){
			fragmentIndex=0;
		}else{
			fragmentIndex=intent.getIntExtra("fragmentIndex", 0);
		}
		initView();// 加载视图布局
		initData();// 加载数据
		initEvent();//加载事件
	}
	
	private void initEvent() {
		indicator.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				tracker.setScreenName(status[position]);
				tracker.send(screenViewBuilder.build());
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		tracker.setScreenName(status[orderViewPager.getCurrentItem()]);
		tracker.send(screenViewBuilder.build());
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		tracker.setScreenName(null);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		orderViewPager = null;
		indicator = null;
		tabs = null;
	}

	/**
	 * 加载数据
	 */
	private void initData() {
		for (int i = 0; i < tabs.length; i++) {
			MyOrderFragment orderFragment = new MyOrderFragment(this, status[i]);
			orderFragment.setOrderOperateCompleteListenner(this);
			newArrivalFragmentsLists.add(orderFragment);
		}
		// 设置头部显示信息
		setTitleContent(R.string.myorder_toptitle);
		setLeftBtnContent(true, -1);
		adapter = new MyOrderAdapter(getSupportFragmentManager(), tabs,
				newArrivalFragmentsLists);
		orderViewPager.setAdapter(adapter);
		//orderViewPager.setOffscreenPageLimit(0);
		indicator.setViewPager(orderViewPager);
		indicator.setTextColor(
				getResources().getColor(R.color.myorder_tab_text_normal),
				getResources().getColor(R.color.myorder_tab_text_selected));
		orderViewPager.setCurrentItem(fragmentIndex);
	}

	/**
	 * 加载视图布局
	 */
	private void initView() {
		setContentView(R.layout.activity_myorder);
		orderViewPager = (ViewPager) findViewById(R.id.vp_myorder_orderlist);
		indicator = (PagerSlidingTabStrip) findViewById(R.id.indicator_myorder_tab);
	}

	@Override
	public void refreshOrderStatus(int oreateType, String fragmentType) {
		if (null == newArrivalFragmentsLists
				|| newArrivalFragmentsLists.size() != 4) {
			return;
		}
		switch (oreateType) {
		case 0:// 取消动作
				// newArrivalFragmentsLists.get(0)
			if (status[0].equals(fragmentType)) {
				SystemClock.sleep(30);
				newArrivalFragmentsLists.get(1).refreshOrderStatus(false,oreateType);
				newArrivalFragmentsLists.get(2).setView();
			} else if (status[1].equals(fragmentType)) {
				newArrivalFragmentsLists.get(0).refreshOrderStatus(false,oreateType);
			} else if (status[2].equals(fragmentType)) {
				newArrivalFragmentsLists.get(0).setView();
			}
			break;
		case 1: // 支付动作
			if (status[0].equals(fragmentType)) {
				newArrivalFragmentsLists.get(2).setView();
				SystemClock.sleep(30);
				newArrivalFragmentsLists.get(1).refreshOrderStatus(false,oreateType);
			} else if (status[1].equals(fragmentType)) {
				SystemClock.sleep(30);
				newArrivalFragmentsLists.get(0).refreshOrderStatus(false,oreateType);
				SystemClock.sleep(30);
				newArrivalFragmentsLists.get(2).refreshOrderStatus(false,oreateType);
			}
			break;
		case 2: // 确认收货动作
			if (status[0].equals(fragmentType)) {
				newArrivalFragmentsLists.get(3).setView();
			} else if (status[3].equals(fragmentType)) {
				newArrivalFragmentsLists.get(0).setView();
			}
			break;

		default:
			break;
		}
	}

}
